package edu.mtu.citizenscience.ethnography.uploader;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import edu.mtu.citizenscience.ethnography.util.Util;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

class EthnographyRegisterAction implements EthnographyUploaderAction {

	private static final String LOG_TAG = "EthnographyRegisterAction";
	
	private boolean hasNext = true;
	private String resource = "User_API/registerNewUser";
	
	private String username;
	
	private Context context;
	private EthnographyResponseHandler handler;
	
	public EthnographyRegisterAction(Context context) {
		this.context = context;
		handler = new EthnographyRegisterHandler();
	}
	
	@Override
	public Map<String, Object> getParams(Intent intent) {
		// TODO Auto-generated method stub
		String[] inputs = {EthnographyUploadService.INPUT_USERNAME,
		                   EthnographyUploadService.INPUT_PASSWORD,
		                   EthnographyUploadService.INPUT_FIRST_NAME,
		                   EthnographyUploadService.INPUT_LAST_NAME,
		                   EthnographyUploadService.INPUT_EMAIL};
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		for (String inputName: inputs) {
			String input = intent.getStringExtra(inputName);
			if (input == null) {
				return null;
			}
			params.put(inputName, input);
		}
		
		username = intent.getStringExtra(EthnographyUploadService.INPUT_USERNAME);
		
		return params;
	}

	@Override
	public String getResource() {
		return resource;
	}

	@Override
	public EthnographyResponseHandler getHandler() {
		return handler;
	}

	private class EthnographyRegisterHandler implements
			EthnographyResponseHandler {
	
		private LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(context);
		
		private static final String RESPONSE_API_KEY = "apiKey";
		
		@Override
		public void onSuccess(JSONObject json) {
			// TODO Auto-generated method stub
			Log.d(LOG_TAG, json.toString());
			
			String key;
			try {
				key = json.getString(RESPONSE_API_KEY);
			} catch (JSONException e) {
				onFailure(json);
				return;
			}
			if (key == null) {
				onFailure(json);
				return;
			}
			
			// Save the result to shared preferences
			SharedPreferences prefs = context.getSharedPreferences(Util.PREFS_KEY, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			
			editor.putString(Util.PREFS_USERNAME, username);
			editor.putString(Util.PREFS_API_KEY, key);
			editor.commit();
			
			hasNext = false;
			
			// Send a broadcast indicating success
			Intent intent = new Intent(EthnographyUploadService.UPLOAD_SERVICE);
			intent.putExtra(EthnographyUploadService.OUTPUT_RESULT, EthnographyUploadService.RESULT_SUCCESS);
			broadcastManager.sendBroadcast(intent);
		}
	
		@Override
		public void onError(JSONObject json) {
			Intent intent = new Intent(EthnographyUploadService.UPLOAD_SERVICE);
			hasNext = false;
			intent.putExtra(EthnographyUploadService.OUTPUT_RESULT, EthnographyUploadService.RESULT_ERROR);
			broadcastManager.sendBroadcast(intent);
		}
	
		@Override
		public void onFailure(JSONObject json) {
			Intent intent = new Intent(EthnographyUploadService.UPLOAD_SERVICE);
			hasNext = false;
			intent.putExtra(EthnographyUploadService.OUTPUT_RESULT, EthnographyUploadService.RESULT_FAILURE);
			broadcastManager.sendBroadcast(intent);
		}
	
		@Override
		public void onNoResponse(JSONObject json) {
			Intent intent = new Intent(EthnographyUploadService.UPLOAD_SERVICE);
			hasNext = false;
			intent.putExtra(EthnographyUploadService.OUTPUT_RESULT, EthnographyUploadService.RESULT_NO_RESPONSE);
			broadcastManager.sendBroadcast(intent);
		}
	
	}

	@Override
	public boolean hasNext() {
		return hasNext;
	}

}
